﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Frame.Lib.Core.Role
{
    class RoleDAL : BaseDAL
    {
        /// <summary>
        /// 获得某一角色下的所有模块ID
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<int> GetModuleIDs(int roleID)
        {
            string sql = "select ModuleID from " + STableName.ModuleRole + " where RoleID = @p0";
            List<int> ids = dbOper.Query<int>(sql, roleID);
            return ids;
        }

        /// <summary>
        /// 获得一组角色下的所有模块ID
        /// </summary>
        /// <param name="lstRoleIDs"></param>
        /// <returns></returns>
        public List<int> GetModuleIDs(IEnumerable<int> lstRoleIDs)
        {
            string roleIDs = ConvertUtil.ConvertToDbStr(lstRoleIDs);
            string sql = "select ModuleID from " + STableName.ModuleRole + " where RoleID in (" + roleIDs + ")";
            List<int> ids = dbOper.Query<int>(sql);
            return ids;
        }

        /// <summary>
        /// 保存一角色拥有的模块ID
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="lstModuleIDs"></param>
        public void SaveModuleIDs(int roleID, List<int> lstModuleIDs)
        {
            string[] sqls = new string[lstModuleIDs.Count() + 1];
            sqls[0] = "delete from " + STableName.ModuleRole + " where RoleID = " + roleID;

            for (int i = 0; i < lstModuleIDs.Count(); i++)
            {
                sqls[i + 1] = "insert into " + STableName.ModuleRole + "(ModuleID, RoleID) values(" + lstModuleIDs[i] + "," + roleID + ")";
            }

            dbOper.RunSql(sqls);               
        }

        public void SaveModuleIDs(List<int> lstRoleIDs, int moduleID)
        {

            string[] sqls = new string[lstRoleIDs.Count() + 1];
            sqls[0] = "delete from "  + STableName.ModuleRole + " where ModuleID = " + moduleID;

            for (int i = 0; i < lstRoleIDs.Count; i++)
            {
                sqls[i + 1] = "insert into " + STableName.ModuleRole + "(ModuleID, RoleID) values(" + moduleID + "," + lstRoleIDs[i] + ")";
            }

            dbOper.RunSql(sqls);    
           
        }

        /// <summary>
        /// 保存一角色拥有的用户ID
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="lstUserIDs"></param>
        public void SaveUserIDs(int roleID, List<int> lstUserIDs)
        {
            string[] sqls = new string[lstUserIDs.Count() + 1];
            sqls[0] = "delete from " + STableName.RoleUser + " where RoleID = " + roleID;

            for (int i = 0; i < lstUserIDs.Count; i++)
            {
                sqls[i + 1] = "insert into " + STableName.RoleUser + "(RoleID, UserID) values(" + roleID + "," + lstUserIDs[i] + ")";
            }

            dbOper.RunSql(sqls); 
        }

        public void Delete(IEnumerable<int> ids)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(ids);
            string strSql = "delete from " + STableName.ModuleRole + " where RoleID in (" + strIDs + ");" +
                            "delete from " + STableName.RoleUser + " where RoleID in (" + strIDs + ");" +
                            "delete from " + STableName.Role + " where ID in (" + strIDs + ")";
            dbOper.RunSql(strSql);
        }

        public void SaveUserIDs(List<int> lstRoleIDs, int userID)
        {
            string[] sqls = new string[lstRoleIDs.Count + 1];
            sqls[0] = "delete from " + STableName.RoleUser + " where UserID = " + userID;

            for (int i = 0; i < lstRoleIDs.Count; i++)
            {
                sqls[i + 1] = "insert into " + STableName.RoleUser + "(RoleID, UserID) values(" + lstRoleIDs[i] + "," + userID + ")";
            }

            dbOper.RunSql(sqls); 
        }

        /// <summary>
        /// 获得一角色下的所有用户ID
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<int> GetUserIDs(int roleID)
        {
            string sql = "select UserID from " + STableName.RoleUser + " where RoleID = @p0";

            List<int> userIDs = dbOper.Query<int>(sql, roleID);
            return userIDs;
        }

        internal List<int> GetUserIDs(List<int> roleIDs)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(roleIDs);
            string sql = "select UserID from " + STableName.RoleUser + " where RoleID in (" + strIDs + ")";
            List<int> userIDs = dbOper.Query<int>(sql);
            return userIDs;
        }

        public void AddModuleID(int roleID, int moduleID)
        {
            string sql = "insert into " + STableName.ModuleRole + "(ModuleID, RoleID) values(@p0, @p1)";
            dbOper.RunSql(sql, moduleID, roleID);          
        }



        public void AddUserID(int roleID, int userID)
        {
            string sql = "insert into " + STableName.RoleUser + "(RoleID, UserID) values(@p0, @p1)";
            dbOper.RunSql(sql, roleID, userID);
          
        }

        public List<RoleInfo> GetList(params RoleType[] roleTypes)
        {
            if (roleTypes.Length > 0)
            {
                string typeIDs = "";
                foreach (RoleType type in roleTypes)
                {
                    typeIDs += ((int)type).ToString() + ",";
                }
                typeIDs = typeIDs.TrimEnd(',');
                string sql = "select * from " + STableName.Role + " where TypeID in (" + typeIDs + ")";
                List<RoleInfo> list = dbOper.Query<RoleInfo>(sql);
                return list;
            }
            else
            {
                return new List<RoleInfo>();
            }
        }

        
    }
}
